#include<stdio.h>
#include<conio.h>
#include<math.h>
#define leng 100
int x,d;
int a[leng];
int e_gcd(int e, int m)
{
				int s;
				d=1;
				do
				{
								s=(d*e)%m;
								d++;
				} while(s!=1);
				d-=1;
				return d;
}				
int array()
{
				printf("\nInput x:");
				scanf("%d",&x);
				for(int i=0;i<x;i++)
				{
        printf("\na[%d]=",i);
        scanf("%d",&a[i]);
				}
}			
int encrypt(int C, int M, int n)
{
				C=1;
				for(int i=0;i<x;i++)
				{
        C*=C;
								C=C%n;
								if (a[i]==1)
								C=C*(M%n);
								C=C%n;
				}
				printf("Encrypted key: %d",C);
}
int decrypt(int C, int M, int n)
{
				M=1;
				for(int i=0;i<x;i++)
				{
                                M*=M;
                                printf("\n%d",M);
								M=M%n;
								printf("\n%d",M);
								if (a[i]==1)
								M=M*(C%n);
								printf("\n%d",M);
								M=M%n;
				}
				printf("\nDecrypted key: %d",M);
}
int main()
{
				int p,n,C,M,m,e,q;
				printf("\nInput 2 large prime numbers:");
				printf("\nEnter p:");
				scanf("%d",&p);
				printf("\nEnter q:");
				scanf("%d",&q);
				printf("Enter e:");
				scanf("%d",&e);
				n=p*q; m=(p-1)*(q-1);
				printf("\nm=%d",m);
				array();
				e_gcd(e,m);
				printf("\nPublic key: <%d,%d> --- Private Key: <%d,%d>",e,n,d,n);
				printf("\nEnter the plaintext M:");
				scanf("%d",&M);
				encrypt(C,M,n);
				printf("\nEnter the plaintext C:");
				scanf("%d",&C);
				decrypt(C,M,n);
				getch();				
}
